package com.shop.cereshop.business.dl.logic.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.business.dao.buyer.CereBuyerUserDAO;
import com.shop.cereshop.business.dao.distributor.CereShopCommissionRecordDao;
import com.shop.cereshop.business.dao.distributor.CereShopDistributorDAO;
import com.shop.cereshop.business.dao.dl.CereBuyerWithdrawalDAO;
import com.shop.cereshop.business.dl.logic.domain.BuyerWithdrawal;
import com.shop.cereshop.business.dl.logic.domain.BuyerWithdrawalGetAllParam;
import com.shop.cereshop.business.dl.logic.domain.BuyerWithdrawalGetByIdParam;
import com.shop.cereshop.business.dl.logic.domain.CereShopCommissionRecord;
import com.shop.cereshop.business.dl.logic.service.CereBuyerWithdrawalService;
import com.shop.cereshop.business.dl.logic.service.ChangeService;
import com.shop.cereshop.business.dl.logic.service.log.CerePlatformLogService;
import com.shop.cereshop.business.page.buyer.BuyerUserDetail;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.business.CerePlatformBusiness;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.buyer.CereBuyerWithdrawal;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.distributor.CereShopDistributor;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Service
public class CereBuyerWithdrawalServiceImpl implements CereBuyerWithdrawalService {

    @Value("${Detail.expenditure.withdraw}")
    private String withdraw;

    @Autowired
    private CereBuyerWithdrawalDAO cereBuyerWithdrawalDAO;

    @Autowired
    private CereShopDistributorDAO cereShopDistributorDAO;
    @Autowired
    private CereBuyerUserDAO cereBuyerUserDAO;

    @Autowired
    private CereShopCommissionRecordDao cereShopCommissionRecordDao;

    @Autowired
    private CerePlatformLogService cerePlatformLogService;
    @Autowired
    private ChangeService changeService;

    @Override
    public Page getAll(BuyerWithdrawalGetAllParam param) throws CoBusinessException {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        List<BuyerWithdrawal> list=cereBuyerWithdrawalDAO.getAll(param);
        PageInfo<BuyerWithdrawal> pageInfo=new PageInfo<>(list);
        Page page=new Page(pageInfo.getList(),pageInfo.getTotal());
        return page;
    }

    @Override
    public BuyerWithdrawal getById(Long withdrawalId) throws CoBusinessException {
        return cereBuyerWithdrawalDAO.getById(withdrawalId);
    }

    @Override
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = {CoBusinessException.class, Exception.class})
    public void handle(HttpServletRequest request, BuyerWithdrawalGetByIdParam param, CerePlatformBusiness user, CereBuyerUser user2) throws CoBusinessException {
        BuyerWithdrawal b1 = cereBuyerWithdrawalDAO.getById(param.getWithdrawalId());
        //todo
        if (param.getType()==1 && param.getState()==1){
//        if (user2.getPhone().equals("18870913915")){
            changeService.transfer(param,request,b1.getWithdrawalMoney(),user,user2.getWechatOpenId(),user2);
        }else {
            String time = TimeUtils.yyMMddHHmmss();
            String describe = "";
            CereBuyerWithdrawal cereBuyerWithdrawal = new CereBuyerWithdrawal();
            cereBuyerWithdrawal.setWithdrawalId(param.getWithdrawalId());
            CereBuyerWithdrawal byId = cereBuyerWithdrawalDAO.selectByPrimaryKey(param.getWithdrawalId());
            if (IntegerEnum.WITHDRAWAL_SUCCESS.getCode().equals(param.getState())) {
                describe = "同意用户提现申请";

                //...查询分销员信息,需要分销员表中用户id不为空
                CereShopDistributor cereShopDistributor = cereShopDistributorDAO.findByBuyerUserId(byId.getBuyerUserId());
                //添加提现支出记录
                CereShopCommissionRecord record = new CereShopCommissionRecord();
                record.setCommission(byId.getWithdrawalMoney().multiply(new BigDecimal(-1))
                );
                record.setState(2);
                record.setDistributorId(cereShopDistributor.getDistributorId());
                record.setDetail(withdraw);
                record.setCreateTime(time);
                cereShopCommissionRecordDao.insertSelective(record);

                cereBuyerWithdrawal.setState(IntegerEnum.WITHDRAWAL_SUCCESS.getCode());
            } else {
                describe = "拒绝用户提现申请";
                cereBuyerWithdrawal.setState(IntegerEnum.WITHDRAWAL_ERROR.getCode());
            }
            cereBuyerWithdrawal.setHandleTime(time);
            cereBuyerWithdrawalDAO.updateByPrimaryKeySelective(cereBuyerWithdrawal);
            //新增日志
            cerePlatformLogService.addLog(user, "用户提现申请", "商家端操作", describe, String.valueOf(param.getWithdrawalId()), time);
        }
    }
}
